<?php
/**
 * Clase Base DP para BaseImArticuloDP
 *
 * @package    
 * @subpackage BaseImArticuloDP
 * @author     hans
 * @version    SVN:: BaseImArticuloDP.class.php,v 1.1 2012-03-16 23:50:50 Hans.parra Exp $
 */

include_once('lib/common/arrayClass.class.php');
require_once('class/im/articulos/BN/ImArticuloBN.class.php');

abstract class BaseImArticuloDP extends ImArticuloBN
{
  /* N�mero de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof ImArticuloBN )
    {
      $o_ImArticuloBN = $bSetDefault;
      if($o_ImArticuloBN->getArticuloId()) : $this->setArticuloId($o_ImArticuloBN->getArticuloId()); endif;
      if($o_ImArticuloBN->getTipo()) : $this->setTipo($o_ImArticuloBN->getTipo()); endif;
      if($o_ImArticuloBN->getDescripcion()) : $this->setDescripcion($o_ImArticuloBN->getDescripcion()); endif;
      if($o_ImArticuloBN->getMemo()) : $this->setMemo($o_ImArticuloBN->getMemo()); endif;
      if($o_ImArticuloBN->getAlias()) : $this->setAlias($o_ImArticuloBN->getAlias()); endif;
      if($o_ImArticuloBN->getCodFabricante()) : $this->setCodFabricante($o_ImArticuloBN->getCodFabricante()); endif;
      if($o_ImArticuloBN->getClasificId()) : $this->setClasificId($o_ImArticuloBN->getClasificId()); endif;
      if($o_ImArticuloBN->getSubClasificId()) : $this->setSubClasificId($o_ImArticuloBN->getSubClasificId()); endif;
      if($o_ImArticuloBN->getMarcaId()) : $this->setMarcaId($o_ImArticuloBN->getMarcaId()); endif;
      if($o_ImArticuloBN->getProveed1()) : $this->setProveed1($o_ImArticuloBN->getProveed1()); endif;
      if($o_ImArticuloBN->getProveed2()) : $this->setProveed2($o_ImArticuloBN->getProveed2()); endif;
      if($o_ImArticuloBN->getUndMedida()) : $this->setUndMedida($o_ImArticuloBN->getUndMedida()); endif;
      if($o_ImArticuloBN->getUndMedidaCompra()) : $this->setUndMedidaCompra($o_ImArticuloBN->getUndMedidaCompra()); endif;
      if($o_ImArticuloBN->getUoMFactor()) : $this->setUoMFactor($o_ImArticuloBN->getUoMFactor()); endif;
      if($o_ImArticuloBN->getUoMVenta()) : $this->setUoMVenta($o_ImArticuloBN->getUoMVenta()); endif;
      if($o_ImArticuloBN->getUoMVentaFactor()) : $this->setUoMVentaFactor($o_ImArticuloBN->getUoMVentaFactor()); endif;
      if($o_ImArticuloBN->getGarantia()) : $this->setGarantia($o_ImArticuloBN->getGarantia()); endif;
      if($o_ImArticuloBN->getInventariable()) : $this->setInventariable($o_ImArticuloBN->getInventariable()); endif;
      if($o_ImArticuloBN->getSerializable()) : $this->setSerializable($o_ImArticuloBN->getSerializable()); endif;
      if($o_ImArticuloBN->getParaCompra()) : $this->setParaCompra($o_ImArticuloBN->getParaCompra()); endif;
      if($o_ImArticuloBN->getParaVenta()) : $this->setParaVenta($o_ImArticuloBN->getParaVenta()); endif;
      if($o_ImArticuloBN->getImpuesto1Sw()) : $this->setImpuesto1Sw($o_ImArticuloBN->getImpuesto1Sw()); endif;
      if($o_ImArticuloBN->getImpuesto2Sw()) : $this->setImpuesto2Sw($o_ImArticuloBN->getImpuesto2Sw()); endif;
      if($o_ImArticuloBN->getImpuesto3Sw()) : $this->setImpuesto3Sw($o_ImArticuloBN->getImpuesto3Sw()); endif;
      if($o_ImArticuloBN->getAdvalorem()) : $this->setAdvalorem($o_ImArticuloBN->getAdvalorem()); endif;
      if($o_ImArticuloBN->getPeso()) : $this->setPeso($o_ImArticuloBN->getPeso()); endif;
      if($o_ImArticuloBN->getVolumen()) : $this->setVolumen($o_ImArticuloBN->getVolumen()); endif;
      if($o_ImArticuloBN->getPrecioDefault()) : $this->setPrecioDefault($o_ImArticuloBN->getPrecioDefault()); endif;
      if($o_ImArticuloBN->getCostoProm()) : $this->setCostoProm($o_ImArticuloBN->getCostoProm()); endif;
      if($o_ImArticuloBN->getCostoUlt()) : $this->setCostoUlt($o_ImArticuloBN->getCostoUlt()); endif;
      if($o_ImArticuloBN->getStockMin()) : $this->setStockMin($o_ImArticuloBN->getStockMin()); endif;
      if($o_ImArticuloBN->getStockMax()) : $this->setStockMax($o_ImArticuloBN->getStockMax()); endif;
      if($o_ImArticuloBN->getTiempoReposic()) : $this->setTiempoReposic($o_ImArticuloBN->getTiempoReposic()); endif;
      if($o_ImArticuloBN->getActivo()) : $this->setActivo($o_ImArticuloBN->getActivo()); endif;
      if($o_ImArticuloBN->getCreaUsr()) : $this->setCreaUsr($o_ImArticuloBN->getCreaUsr()); endif;
      if($o_ImArticuloBN->getCreaFecha()) : $this->setCreaFecha($o_ImArticuloBN->getCreaFecha()); endif;
      if($o_ImArticuloBN->getModifUsr()) : $this->setModifUsr($o_ImArticuloBN->getModifUsr()); endif;
      if($o_ImArticuloBN->getModifFecha()) : $this->setModifFecha($o_ImArticuloBN->getModifFecha()); endif;
    }
  }

  /* Obtiene el n�mero de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT IF(MAX(itm_articuloId),(MAX(itm_articuloId)+1), 1) AS Maximo FROM im_articulo;";
      $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
      $result = mysql_query($sql);
      $row = mysql_fetch_array($result);
      //$this->setArticuloId($row[0]); codigo de articulo no sera autogenerado

      if($this->getArticuloId()){
        $a_Fields = array();

        if($this->getArticuloId()) : $a_Fields['itm_articuloId'] = ($this->getArticuloId()); endif;
        if($this->getTipo()) : $a_Fields['itm_tipo'] = ($this->getTipo()); endif;
        if($this->getDescripcion()) : $a_Fields['itm_descripcion'] = ($this->getDescripcion()); endif;
        if($this->getMemo()) : $a_Fields['itm_memo'] = ($this->getMemo()); endif;
        if($this->getAlias()) : $a_Fields['itm_alias'] = ($this->getAlias()); endif;
        if($this->getCodFabricante()) : $a_Fields['itm_codFabricante'] = ($this->getCodFabricante()); endif;
        if($this->getClasificId()) : $a_Fields['itm_clasificId'] = ($this->getClasificId()); endif;
        if($this->getSubClasificId()) : $a_Fields['itm_subClasificId'] = ($this->getSubClasificId()); endif;
        if($this->getMarcaId()) : $a_Fields['itm_marcaId'] = ($this->getMarcaId()); endif;
        if($this->getProveed1()) : $a_Fields['itm_proveed1'] = ($this->getProveed1()); endif;
        if($this->getProveed2()) : $a_Fields['itm_proveed2'] = ($this->getProveed2()); endif;
        if($this->getUndMedida()) : $a_Fields['itm_UoM'] = ($this->getUndMedida()); endif;
        if($this->getUndMedidaCompra()) : $a_Fields['itm_UoMCompra'] = ($this->getUndMedidaCompra()); endif;
        if($this->getUoMFactor()) : $a_Fields['itm_UoMCompraFacto'] = ($this->getUoMFactor()); endif;
        if($this->getUoMVenta()) : $a_Fields['itm_UoMVenta'] = ($this->getUoMVenta()); endif;
        if($this->getUoMVentaFactor()) : $a_Fields['itm_UoMVentaFacto'] = ($this->getUoMVentaFactor()); endif;
        if($this->getGarantia()) : $a_Fields['itm_garantia'] = ($this->getGarantia()); endif;
        if($this->getInventariable()) : $a_Fields['itm_inventariable'] = ($this->getInventariable()); endif;
        if($this->getSerializable()) : $a_Fields['itm_serializable'] = ($this->getSerializable()); endif;
        if($this->getParaCompra()) : $a_Fields['itm_paraCompra'] = ($this->getParaCompra()); endif;
        if($this->getParaVenta()) : $a_Fields['itm_paraVenta'] = ($this->getParaVenta()); endif;
        if($this->getImpuesto1Sw()) : $a_Fields['itm_impuesto1_sw'] = ($this->getImpuesto1Sw()); endif;
        if($this->getImpuesto2Sw()) : $a_Fields['itm_impuesto2_sw'] = ($this->getImpuesto2Sw()); endif;
        if($this->getImpuesto3Sw()) : $a_Fields['itm_impuesto3_sw'] = ($this->getImpuesto3Sw()); endif;
        if($this->getAdvalorem()) : $a_Fields['itm_advalorem'] = ($this->getAdvalorem()); endif;
        if($this->getPeso()) : $a_Fields['itm_peso'] = ($this->getPeso()); endif;
        if($this->getVolumen()) : $a_Fields['itm_volumen'] = ($this->getVolumen()); endif;
        if($this->getPrecioDefault()) : $a_Fields['itm_precioDefault'] = ($this->getPrecioDefault()); endif;
        if($this->getCostoProm()) : $a_Fields['itm_costoProm'] = ($this->getCostoProm()); endif;
        if($this->getCostoUlt()) : $a_Fields['itm_costoUlt'] = ($this->getCostoUlt()); endif;
        if($this->getStockMin()) : $a_Fields['itm_stockMin'] = ($this->getStockMin()); endif;
        if($this->getStockMax()) : $a_Fields['itm_stockMax'] = ($this->getStockMax()); endif;
        if($this->getTiempoReposic()) : $a_Fields['itm_tiempoReposic'] = ($this->getTiempoReposic()); endif;
        if($this->getActivo()) : $a_Fields['itm_activo'] = ($this->getActivo()); endif;
        if($this->getCreaUsr()) : $a_Fields['itm_crea_usr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['itm_crea_fecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['itm_modif_usr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['itm_modif_fecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO im_articulo (".$sStringField.") VALUES (".$sStringValue.");";
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
      	$result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getArticuloId())
      {
        $a_Fields = array();

        if($this->getArticuloId()) : $a_Fields['itm_articuloId'] = ($this->getArticuloId()); endif;
        if($this->getTipo()) : $a_Fields['itm_tipo'] = ($this->getTipo()); endif;
        if($this->getDescripcion()) : $a_Fields['itm_descripcion'] = ($this->getDescripcion()); endif;
        if($this->getMemo()) : $a_Fields['itm_memo'] = ($this->getMemo()); endif;
        if($this->getAlias()) : $a_Fields['itm_alias'] = ($this->getAlias()); endif;
        if($this->getCodFabricante()) : $a_Fields['itm_codFabricante'] = ($this->getCodFabricante()); endif;
        if($this->getClasificId()) : $a_Fields['itm_clasificId'] = ($this->getClasificId()); endif;
        if($this->getSubClasificId()) : $a_Fields['itm_subClasificId'] = ($this->getSubClasificId()); endif;
        if($this->getMarcaId()) : $a_Fields['itm_marcaId'] = ($this->getMarcaId()); endif;
        if($this->getProveed1()) : $a_Fields['itm_proveed1'] = ($this->getProveed1()); endif;
        if($this->getProveed2()) : $a_Fields['itm_proveed2'] = ($this->getProveed2()); endif;
        if($this->getUndMedida()) : $a_Fields['itm_UoM'] = ($this->getUndMedida()); endif;
        if($this->getUoM()) : $a_Fields['itm_UoM'] = ($this->getUoM()); endif;        
        if($this->getUoMCompra()) : $a_Fields['itm_UoMCompra'] = ($this->getUoMCompra()); endif;
        if($this->getUndMedidaCompra()) : $a_Fields['itm_UoMCompra'] = ($this->getUndMedidaCompra()); endif;
        if($this->getUoMFactor()) : $a_Fields['itm_UoMCompraFacto'] = ($this->getUoMFactor()); endif;
        if($this->getUoMVenta()) : $a_Fields['itm_UoMVenta'] = ($this->getUoMVenta()); endif;
        if($this->getUoMCompraFactor()) : $a_Fields['itm_UoMCompraFacto'] = ($this->getUoMCompraFactor()); endif;
        if($this->getUoMVentaFactor()) : $a_Fields['itm_UoMVentaFacto'] = ($this->getUoMVentaFactor()); endif;        
        if($this->getGarantia()) : $a_Fields['itm_garantia'] = ($this->getGarantia()); endif;
        if($this->getInventariable()) : $a_Fields['itm_inventariable'] = ($this->getInventariable()); endif;
        if($this->getSerializable()) : $a_Fields['itm_serializable'] = ($this->getSerializable()); endif;
        if($this->getParaCompra()) : $a_Fields['itm_paraCompra'] = ($this->getParaCompra()); endif;
        if($this->getParaVenta()) : $a_Fields['itm_paraVenta'] = ($this->getParaVenta()); endif;
        if($this->getImpuesto1Sw()) : $a_Fields['itm_impuesto1_sw'] = ($this->getImpuesto1Sw()); endif;
        if($this->getImpuesto2Sw()) : $a_Fields['itm_impuesto2_sw'] = ($this->getImpuesto2Sw()); endif;
        if($this->getImpuesto3Sw()) : $a_Fields['itm_impuesto3_sw'] = ($this->getImpuesto3Sw()); endif;
        if($this->getAdvalorem()) : $a_Fields['itm_advalorem'] = ($this->getAdvalorem()); endif;
        if($this->getPeso()) : $a_Fields['itm_peso'] = ($this->getPeso()); endif;
        if($this->getVolumen()) : $a_Fields['itm_volumen'] = ($this->getVolumen()); endif;
        if($this->getPrecioDefault()) : $a_Fields['itm_precioDefault'] = ($this->getPrecioDefault()); endif;
        if($this->getCostoProm()) : $a_Fields['itm_costoProm'] = ($this->getCostoProm()); endif;
        if($this->getCostoUlt()) : $a_Fields['itm_costoUlt'] = ($this->getCostoUlt()); endif;
        if($this->getStockMin()) : $a_Fields['itm_stockMin'] = ($this->getStockMin()); endif;
        if($this->getStockMax()) : $a_Fields['itm_stockMax'] = ($this->getStockMax()); endif;
        if($this->getTiempoReposic()) : $a_Fields['itm_tiempoReposic'] = ($this->getTiempoReposic()); endif;
        if($this->getActivo()) : $a_Fields['itm_activo'] = ($this->getActivo()); endif;
        if($this->getCreaUsr()) : $a_Fields['itm_crea_usr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['itm_crea_fecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['itm_modif_usr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['itm_modif_fecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

        $o_conexion = new BaseDatos();
        $con=$o_conexion->conectar();
        $sql = "UPDATE im_articulo SET ".$sStringField." WHERE 1 AND itm_articuloId='".$this->getArticuloId()."';";
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete()
  {
    try
    {
      if($this->getArticuloId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
        $con=$o_conexion->conectar();
        $sql = "DELETE FROM im_articulo WHERE 1 AND itm_articuloId='".$this->getArticuloId()."';"; 
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* B�squeda del registro por la llave primaria */
  public static function selectByPK($vArticuloId)
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT itm_articuloId, itm_tipo, itm_descripcion, itm_memo, itm_alias, itm_codFabricante, itm_clasificId, itm_subClasificId, itm_marcaId, itm_proveed1, itm_proveed2, itm_UoM, itm_UoMCompra, itm_UoMCompraFacto, itm_UoMVenta, itm_UoMVentaFacto, itm_garantia, itm_inventariable, itm_serializable, itm_paraCompra, itm_paraVenta, itm_impuesto1_sw, itm_impuesto2_sw, itm_impuesto3_sw, itm_advalorem, itm_peso, itm_volumen, itm_precioDefault, itm_costoProm, itm_costoUlt, itm_stockMin, itm_stockMax, itm_tiempoReposic, itm_activo, itm_crea_usr, itm_crea_fecha, itm_modif_usr, itm_modif_fecha FROM im_articulo WHERE 1 AND itm_articuloId='".$vArticuloId."'; ";
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_ImArticuloDP = new ImArticuloBN();
    $o_ImArticuloDP->setArticuloId($o_propel->itm_articuloId);
    $o_ImArticuloDP->setTipo($o_propel->itm_tipo);
    $o_ImArticuloDP->setDescripcion($o_propel->itm_descripcion);
    $o_ImArticuloDP->setMemo($o_propel->itm_memo);
    $o_ImArticuloDP->setAlias($o_propel->itm_alias);
    $o_ImArticuloDP->setCodFabricante($o_propel->itm_codFabricante);
    $o_ImArticuloDP->setClasificId($o_propel->itm_clasificId);
    $o_ImArticuloDP->setSubClasificId($o_propel->itm_subClasificId);
    $o_ImArticuloDP->setMarcaId($o_propel->itm_marcaId);
    $o_ImArticuloDP->setProveed1($o_propel->itm_proveed1);
    $o_ImArticuloDP->setProveed2($o_propel->itm_proveed2);
    $o_ImArticuloDP->setUndMedida($o_propel->itm_UoM);
    $o_ImArticuloDP->setUndMedidaCompra($o_propel->itm_UoMCompra);
    $o_ImArticuloDP->setUoMCompraFactor($o_propel->itm_UoMCompraFacto);
    $o_ImArticuloDP->setUoMVenta($o_propel->itm_UoMVenta);
    $o_ImArticuloDP->setUoMVentaFactor($o_propel->itm_UoMVentaFacto);
    $o_ImArticuloDP->setGarantia($o_propel->itm_garantia);
    $o_ImArticuloDP->setInventariable($o_propel->itm_inventariable);
    $o_ImArticuloDP->setSerializable($o_propel->itm_serializable);
    $o_ImArticuloDP->setParaCompra($o_propel->itm_paraCompra);
    $o_ImArticuloDP->setParaVenta($o_propel->itm_paraVenta);
    $o_ImArticuloDP->setImpuesto1Sw($o_propel->itm_impuesto1_sw);
    $o_ImArticuloDP->setImpuesto2Sw($o_propel->itm_impuesto2_sw);
    $o_ImArticuloDP->setImpuesto3Sw($o_propel->itm_impuesto3_sw);
    $o_ImArticuloDP->setAdvalorem($o_propel->itm_advalorem);
    $o_ImArticuloDP->setPeso($o_propel->itm_peso);
    $o_ImArticuloDP->setVolumen($o_propel->itm_volumen);
    $o_ImArticuloDP->setPrecioDefault($o_propel->itm_precioDefault);
    $o_ImArticuloDP->setCostoProm($o_propel->itm_costoProm);
    $o_ImArticuloDP->setCostoUlt($o_propel->itm_costoUlt);
    $o_ImArticuloDP->setStockMin($o_propel->itm_stockMin);
    $o_ImArticuloDP->setStockMax($o_propel->itm_stockMax);
    $o_ImArticuloDP->setTiempoReposic($o_propel->itm_tiempoReposic);
    $o_ImArticuloDP->setActivo($o_propel->itm_activo);
    $o_ImArticuloDP->setCreaUsr($o_propel->itm_crea_usr);
    $o_ImArticuloDP->setCreaFecha($o_propel->itm_crea_fecha);
    $o_ImArticuloDP->setModifUsr($o_propel->itm_modif_usr);
    $o_ImArticuloDP->setModifFecha($o_propel->itm_modif_fecha);
    return $o_ImArticuloDP;
  }
}